<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li class="active"><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. dva">1. dva</a></li><li><a href="#t12.数据流向">2.数据流向</a></li><li><a href="#t23. 8个概念">3. 8个概念</a><ul><li><a href="#t33.1 State">3.1 State</a></li><li><a href="#t43.2 Action">3.2 Action</a></li><li><a href="#t53.3 dispatch">3.3 dispatch</a></li><li><a href="#t63.4 Reducer">3.4 Reducer</a></li><li><a href="#t73.5 Effect">3.5 Effect</a></li><li><a href="#t83.6 Subscription">3.6 Subscription</a></li><li><a href="#t93.7 Router">3.7 Router</a></li><li><a href="#t103.8 Route Components">3.8 Route Components</a></li></ul></li><li><a href="#t114. 文件结构">4. 文件结构</a></li><li><a href="#t125. API">5. API</a></li><li><a href="#t136. Model">6. Model</a></li><li><a href="#t147. 测试鼠标点击速度的的应用">7. 测试鼠标点击速度的的应用</a><ul><li><a href="#t157.1 安装dva-cli">7.1 安装dva-cli</a></li><li><a href="#t167.2 创建新应用">7.2 创建新应用</a></li><li><a href="#t177.3  定义模型">7.3  定义模型</a></li><li><a href="#t187.4 完成Component">7.4 完成Component</a></li><li><a href="#t197.5 更新state">7.5 更新state</a></li><li><a href="#t207.6 异步处理">7.6 异步处理</a></li><li><a href="#t217.7 订阅键盘事件">7.7 订阅键盘事件</a></li><li><a href="#t227.8 构建应用">7.8 构建应用</a></li></ul></li><li><a href="#t238. 参考">8. 参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. dva">1. dva <a href="#t01. dva"> # </a></h2>
<ul>
<li>&#x57FA;&#x4E8E; <code>redux</code>&#x3001;<code>redux-saga</code> &#x548C; <code>react-router</code> &#x7684;&#x8F7B;&#x91CF;&#x7EA7;&#x524D;&#x7AEF;&#x6846;&#x67B6;&#x3002;(Inspired by elm and choo)</li>
<li>dva&#x662F;&#x57FA;&#x4E8E;react+redux&#x6700;&#x4F73;&#x5B9E;&#x8DF5;&#x4E0A;&#x5B9E;&#x73B0;&#x7684;&#x5C01;&#x88C5;&#x65B9;&#x6848;&#xFF0C;&#x7B80;&#x5316;&#x4E86;redux&#x548C;redux-saga&#x4F7F;&#x7528;&#x4E0A;&#x7684;&#x8BF8;&#x591A;&#x7E41;&#x7410;&#x64CD;&#x4F5C;</li>
</ul>
<h2 id="t12.&#x6570;&#x636E;&#x6D41;&#x5411;">2.&#x6570;&#x636E;&#x6D41;&#x5411; <a href="#t12.&#x6570;&#x636E;&#x6D41;&#x5411;"> # </a></h2>
<ul>
<li>&#x6570;&#x636E;&#x7684;&#x6539;&#x53D8;&#x53D1;&#x751F;&#x901A;&#x5E38;&#x662F;&#x901A;&#x8FC7;&#xFF1A;<ul>
<li>&#x7528;&#x6237;&#x4EA4;&#x4E92;&#x884C;&#x4E3A;&#xFF08;&#x7528;&#x6237;&#x70B9;&#x51FB;&#x6309;&#x94AE;&#x7B49;&#xFF09;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x884C;&#x4E3A;&#xFF08;&#x5982;&#x8DEF;&#x7531;&#x8DF3;&#x8F6C;&#x7B49;&#xFF09;&#x89E6;&#x53D1;&#x7684;</li>
</ul>
</li>
<li>&#x5F53;&#x6B64;&#x7C7B;&#x884C;&#x4E3A;&#x4F1A;&#x6539;&#x53D8;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; dispatch &#x53D1;&#x8D77;&#x4E00;&#x4E2A; action&#xFF0C;&#x5982;&#x679C;&#x662F;&#x540C;&#x6B65;&#x884C;&#x4E3A;&#x4F1A;&#x76F4;&#x63A5;&#x901A;&#x8FC7; Reducers &#x6539;&#x53D8; State &#xFF0C;&#x5982;&#x679C;&#x662F;&#x5F02;&#x6B65;&#x884C;&#x4E3A;&#xFF08;&#x526F;&#x4F5C;&#x7528;&#xFF09;&#x4F1A;&#x5148;&#x89E6;&#x53D1; Effects &#x7136;&#x540E;&#x6D41;&#x5411; Reducers &#x6700;&#x7EC8;&#x6539;&#x53D8; State</li>
</ul>
<p><img src="https://zos.alipayobjects.com/rmsportal/PPrerEAKbIoDZYr.png" alt="dva-flow"></p>
<h2 id="t23. 8&#x4E2A;&#x6982;&#x5FF5;">3. 8&#x4E2A;&#x6982;&#x5FF5; <a href="#t23. 8&#x4E2A;&#x6982;&#x5FF5;"> # </a></h2>
<h3 id="t33.1 State">3.1 State <a href="#t33.1 State"> # </a></h3>
<ul>
<li>State &#x8868;&#x793A; Model &#x7684;&#x72B6;&#x6001;&#x6570;&#x636E;&#xFF0C;&#x901A;&#x5E38;&#x8868;&#x73B0;&#x4E3A;&#x4E00;&#x4E2A; javascript &#x5BF9;&#x8C61;&#xFF08;&#x5F53;&#x7136;&#x5B83;&#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x4F55;&#x503C;&#xFF09;&#xFF1B;</li>
<li>&#x64CD;&#x4F5C;&#x7684;&#x65F6;&#x5019;&#x6BCF;&#x6B21;&#x90FD;&#x8981;&#x5F53;&#x4F5C;&#x4E0D;&#x53EF;&#x53D8;&#x6570;&#x636E;&#xFF08;immutable data&#xFF09;&#x6765;&#x5BF9;&#x5F85;&#xFF0C;&#x4FDD;&#x8BC1;&#x6BCF;&#x6B21;&#x90FD;&#x662F;&#x5168;&#x65B0;&#x5BF9;&#x8C61;&#xFF0C;&#x6CA1;&#x6709;&#x5F15;&#x7528;&#x5173;&#x7CFB;&#xFF0C;&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x4FDD;&#x8BC1; State &#x7684;&#x72EC;&#x7ACB;&#x6027;&#xFF0C;&#x4FBF;&#x4E8E;&#x6D4B;&#x8BD5;&#x548C;&#x8FFD;&#x8E2A;&#x53D8;&#x5316;&#x3002;</li>
</ul>
<h3 id="t43.2 Action">3.2 Action <a href="#t43.2 Action"> # </a></h3>
<ul>
<li>Action &#x662F;&#x4E00;&#x4E2A;&#x666E;&#x901A; javascript &#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x662F;&#x6539;&#x53D8; State &#x7684;&#x552F;&#x4E00;&#x9014;&#x5F84;&#x3002;</li>
<li>&#x65E0;&#x8BBA;&#x662F;&#x4ECE; UI &#x4E8B;&#x4EF6;&#x3001;&#x7F51;&#x7EDC;&#x56DE;&#x8C03;&#xFF0C;&#x8FD8;&#x662F; WebSocket &#x7B49;&#x6570;&#x636E;&#x6E90;&#x6240;&#x83B7;&#x5F97;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6700;&#x7EC8;&#x90FD;&#x4F1A;&#x901A;&#x8FC7; dispatch &#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E00;&#x4E2A; action&#xFF0C;&#x4ECE;&#x800C;&#x6539;&#x53D8;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x3002;</li>
<li>action &#x5FC5;&#x987B;&#x5E26;&#x6709; type &#x5C5E;&#x6027;&#x6307;&#x660E;&#x5177;&#x4F53;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x5176;&#x5B83;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#xFF0C;</li>
<li>&#x5982;&#x679C;&#x8981;&#x53D1;&#x8D77;&#x4E00;&#x4E2A; action &#x9700;&#x8981;&#x4F7F;&#x7528; dispatch &#x51FD;&#x6570;&#xFF1B;</li>
<li>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F; dispatch &#x662F;&#x5728;&#x7EC4;&#x4EF6; connect Models&#x4EE5;&#x540E;&#xFF0C;&#x901A;&#x8FC7; props &#x4F20;&#x5165;&#x7684;&#x3002;</li>
</ul>
<h3 id="t53.3 dispatch">3.3 dispatch <a href="#t53.3 dispatch"> # </a></h3>
<ul>
<li>dispatching function &#x662F;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x89E6;&#x53D1; action &#x7684;&#x51FD;&#x6570;</li>
<li>action &#x662F;&#x6539;&#x53D8; State &#x7684;&#x552F;&#x4E00;&#x9014;&#x5F84;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x53EA;&#x63CF;&#x8FF0;&#x4E86;&#x4E00;&#x4E2A;&#x884C;&#x4E3A;&#xFF0C;&#x800C; dipatch &#x53EF;&#x4EE5;&#x770B;&#x4F5C;&#x662F;&#x89E6;&#x53D1;&#x8FD9;&#x4E2A;&#x884C;&#x4E3A;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x800C; Reducer &#x5219;&#x662F;&#x63CF;&#x8FF0;&#x5982;&#x4F55;&#x6539;&#x53D8;&#x6570;&#x636E;&#x7684;&#x3002;</li>
<li>&#x5728; dva &#x4E2D;&#xFF0C;connect Model &#x7684;&#x7EC4;&#x4EF6;&#x901A;&#x8FC7; props &#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230; dispatch&#xFF0C;&#x53EF;&#x4EE5;&#x8C03;&#x7528; Model &#x4E2D;&#x7684; Reducer &#x6216;&#x8005; Effects&#xFF0C;&#x5E38;&#x89C1;&#x7684;&#x5F62;&#x5F0F;&#x5982;&#xFF1A;<pre><code class="lang-js">dispatch({
<span class="hljs-attr">type</span>: <span class="hljs-string">&apos;user/add&apos;</span>, <span class="hljs-comment">// &#x5982;&#x679C;&#x5728; model &#x5916;&#x8C03;&#x7528;&#xFF0C;&#x9700;&#x8981;&#x6DFB;&#x52A0; namespace</span>
payload: {}, <span class="hljs-comment">// &#x9700;&#x8981;&#x4F20;&#x9012;&#x7684;&#x4FE1;&#x606F;</span>
});
</code></pre>
</li>
</ul>
<h3 id="t63.4 Reducer">3.4 Reducer <a href="#t63.4 Reducer"> # </a></h3>
<ul>
<li>Reducer&#xFF08;&#x4E5F;&#x79F0;&#x4E3A; reducing function&#xFF09;&#x51FD;&#x6570;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x4E4B;&#x524D;&#x5DF2;&#x7ECF;&#x7D2F;&#x79EF;&#x8FD0;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#x548C;&#x5F53;&#x524D;&#x8981;&#x88AB;&#x7D2F;&#x79EF;&#x7684;&#x503C;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7D2F;&#x79EF;&#x7ED3;&#x679C;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x628A;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x5F52;&#x5E76;&#x6210;&#x4E00;&#x4E2A;&#x5355;&#x503C;&#x3002;</li>
<li>&#x5728; dva &#x4E2D;&#xFF0C;reducers &#x805A;&#x5408;&#x79EF;&#x7D2F;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x5F53;&#x524D; model &#x7684; state &#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x901A;&#x8FC7; actions &#x4E2D;&#x4F20;&#x5165;&#x7684;&#x503C;&#xFF0C;&#x4E0E;&#x5F53;&#x524D; reducers &#x4E2D;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x8FD0;&#x7B97;&#x83B7;&#x5F97;&#x65B0;&#x7684;&#x503C;&#xFF08;&#x4E5F;&#x5C31;&#x662F;&#x65B0;&#x7684; state&#xFF09;&#x3002;</li>
<li>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F; Reducer &#x5FC5;&#x987B;&#x662F;&#x7EAF;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x540C;&#x6837;&#x7684;&#x8F93;&#x5165;&#x5FC5;&#x7136;&#x5F97;&#x5230;&#x540C;&#x6837;&#x7684;&#x8F93;&#x51FA;&#xFF0C;&#x5B83;&#x4EEC;&#x4E0D;&#x5E94;&#x8BE5;&#x4EA7;&#x751F;&#x4EFB;&#x4F55;&#x526F;&#x4F5C;&#x7528;&#x3002;</li>
<li>&#x5E76;&#x4E14;&#xFF0C;&#x6BCF;&#x4E00;&#x6B21;&#x7684;&#x8BA1;&#x7B97;&#x90FD;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;immutable data&#xFF0C;&#x8FD9;&#x79CD;&#x7279;&#x6027;&#x7B80;&#x5355;&#x7406;&#x89E3;&#x5C31;&#x662F;&#x6BCF;&#x6B21;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5168;&#x65B0;&#x7684;&#x6570;&#x636E;&#xFF08;&#x72EC;&#x7ACB;&#xFF0C;&#x7EAF;&#x51C0;&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x70ED;&#x91CD;&#x8F7D;&#x548C;&#x65F6;&#x95F4;&#x65C5;&#x884C;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x624D;&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h3 id="t73.5 Effect">3.5 Effect <a href="#t73.5 Effect"> # </a></h3>
<ul>
<li>Effect &#x88AB;&#x79F0;&#x4E3A;&#x526F;&#x4F5C;&#x7528;&#xFF0C;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x5C31;&#x662F;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x3002;</li>
<li>&#x5B83;&#x6765;&#x81EA;&#x4E8E;&#x51FD;&#x6570;&#x7F16;&#x7A0B;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x4E4B;&#x6240;&#x4EE5;&#x53EB;&#x526F;&#x4F5C;&#x7528;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x7684;&#x51FD;&#x6570;&#x53D8;&#x5F97;&#x4E0D;&#x7EAF;&#xFF0C;&#x540C;&#x6837;&#x7684;&#x8F93;&#x5165;&#x4E0D;&#x4E00;&#x5B9A;&#x83B7;&#x5F97;&#x540C;&#x6837;&#x7684;&#x8F93;&#x51FA;&#x3002;</li>
<li>dva &#x4E3A;&#x4E86;&#x63A7;&#x5236;&#x526F;&#x4F5C;&#x7528;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5E95;&#x5C42;&#x5F15;&#x5165;&#x4E86;redux-sagas&#x505A;&#x5F02;&#x6B65;&#x6D41;&#x7A0B;&#x63A7;&#x5236;&#xFF0C;&#x7531;&#x4E8E;&#x91C7;&#x7528;&#x4E86;generator&#x7684;&#x76F8;&#x5173;&#x6982;&#x5FF5;&#xFF0C;&#x6240;&#x4EE5;&#x5C06;&#x5F02;&#x6B65;&#x8F6C;&#x6210;&#x540C;&#x6B65;&#x5199;&#x6CD5;&#xFF0C;&#x4ECE;&#x800C;&#x5C06;effects&#x8F6C;&#x4E3A;&#x7EAF;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<h3 id="t83.6 Subscription">3.6 Subscription <a href="#t83.6 Subscription"> # </a></h3>
<ul>
<li>Subscriptions &#x662F;&#x4E00;&#x79CD;&#x4ECE; &#x6E90; &#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x6765;&#x81EA;&#x4E8E; elm&#x3002;</li>
<li>Subscription &#x8BED;&#x4E49;&#x662F;&#x8BA2;&#x9605;&#xFF0C;&#x7528;&#x4E8E;&#x8BA2;&#x9605;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x6E90;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x6761;&#x4EF6; dispatch &#x9700;&#x8981;&#x7684; action</li>
<li>&#x6570;&#x636E;&#x6E90;&#x53EF;&#x4EE5;&#x662F;&#x5F53;&#x524D;&#x7684;&#x65F6;&#x95F4;&#x3001;&#x670D;&#x52A1;&#x5668;&#x7684; websocket &#x8FDE;&#x63A5;&#x3001;keyboard &#x8F93;&#x5165;&#x3001;geolocation &#x53D8;&#x5316;&#x3001;history &#x8DEF;&#x7531;&#x53D8;&#x5316;&#x7B49;&#x7B49;&#x3002;</li>
</ul>
<h3 id="t93.7 Router">3.7 Router <a href="#t93.7 Router"> # </a></h3>
<ul>
<li>&#x8FD9;&#x91CC;&#x7684;&#x8DEF;&#x7531;&#x901A;&#x5E38;&#x6307;&#x7684;&#x662F;&#x524D;&#x7AEF;&#x8DEF;&#x7531;</li>
<li>&#x7531;&#x4E8E;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x73B0;&#x5728;&#x901A;&#x5E38;&#x662F;&#x5355;&#x9875;&#x5E94;&#x7528;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x524D;&#x7AEF;&#x4EE3;&#x7801;&#x6765;&#x63A7;&#x5236;&#x8DEF;&#x7531;&#x903B;&#x8F91;</li>
<li>&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x63D0;&#x4F9B;&#x7684; History API &#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x6D4F;&#x89C8;&#x5668;url&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x4ECE;&#x800C;&#x63A7;&#x5236;&#x8DEF;&#x7531;&#x76F8;&#x5173;&#x64CD;&#x4F5C;&#x3002;</li>
</ul>
<h3 id="t103.8 Route Components">3.8 Route Components <a href="#t103.8 Route Components"> # </a></h3>
<ul>
<li>&#x5728;&#x7EC4;&#x4EF6;&#x8BBE;&#x8BA1;&#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x63D0;&#x5230;&#x8FC7; Container Components&#xFF0C;&#x5728; dva &#x4E2D;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x5C06;&#x5176;&#x7EA6;&#x675F;&#x4E3A; Route Components</li>
<li>&#x56E0;&#x4E3A;&#x5728; dva &#x4E2D;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x4EE5;&#x9875;&#x9762;&#x7EF4;&#x5EA6;&#x6765;&#x8BBE;&#x8BA1; Container Components&#x3002;</li>
<li>&#x6240;&#x4EE5;&#x5728; dva &#x4E2D;&#xFF0C;&#x901A;&#x5E38;&#x9700;&#x8981; connect Model&#x7684;&#x7EC4;&#x4EF6;&#x90FD;&#x662F; Route Components&#xFF0C;&#x7EC4;&#x7EC7;&#x5728;/routes/&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x800C;/components/&#x76EE;&#x5F55;&#x4E0B;&#x5219;&#x662F;&#x7EAF;&#x7EC4;&#x4EF6;&#xFF08;Presentational Components&#xFF09;&#x3002;</li>
</ul>
<h2 id="t114. &#x6587;&#x4EF6;&#x7ED3;&#x6784;">4. &#x6587;&#x4EF6;&#x7ED3;&#x6784; <a href="#t114. &#x6587;&#x4EF6;&#x7ED3;&#x6784;"> # </a></h2>
<p>&#x5B98;&#x65B9;&#x63A8;&#x8350;&#x7684;&#xFF1A;</p>
<pre><code class="lang-js">&#x251C;&#x2500;&#x2500; /mock/           # &#x6570;&#x636E;mock&#x7684;&#x63A5;&#x53E3;&#x6587;&#x4EF6;
&#x251C;&#x2500;&#x2500; /src/            # &#x9879;&#x76EE;&#x6E90;&#x7801;&#x76EE;&#x5F55;
&#x2502; &#x251C;&#x2500;&#x2500; /components/   # &#x9879;&#x76EE;&#x7EC4;&#x4EF6;
&#x2502; &#x251C;&#x2500;&#x2500; /routes/       # &#x8DEF;&#x7531;&#x7EC4;&#x4EF6;&#xFF08;&#x9875;&#x9762;&#x7EF4;&#x5EA6;&#xFF09;
&#x2502; &#x251C;&#x2500;&#x2500; /models/       # &#x6570;&#x636E;&#x6A21;&#x578B;
&#x2502; &#x251C;&#x2500;&#x2500; /services/     # &#x6570;&#x636E;&#x63A5;&#x53E3;
&#x2502; &#x251C;&#x2500;&#x2500; /utils/        # &#x5DE5;&#x5177;&#x51FD;&#x6570;
&#x2502; &#x251C;&#x2500;&#x2500; route.js       # &#x8DEF;&#x7531;&#x914D;&#x7F6E;
&#x2502; &#x251C;&#x2500;&#x2500; index.js       # &#x5165;&#x53E3;&#x6587;&#x4EF6;
&#x2502; &#x251C;&#x2500;&#x2500; index.less    
&#x2502; &#x2514;&#x2500;&#x2500; index.html    
&#x251C;&#x2500;&#x2500; package.json     # &#x5B9A;&#x4E49;&#x4F9D;&#x8D56;&#x7684;pkg&#x6587;&#x4EF6;
&#x2514;&#x2500;&#x2500; proxy.config.js  # &#x6570;&#x636E;mock&#x914D;&#x7F6E;&#x6587;&#x4EF6;
</code></pre>
<h2 id="t125. API">5. API <a href="#t125. API"> # </a></h2>
<ul>
<li>app = dva(opts) &#x521B;&#x5EFA;&#x5E94;&#x7528;&#xFF0C;&#x8FD4;&#x56DE; dva &#x5B9E;&#x4F8B;</li>
<li>app.use(hooks) &#x914D;&#x7F6E; hooks &#x6216;&#x8005;&#x6CE8;&#x518C;&#x63D2;&#x4EF6;</li>
<li>app.model(model) &#x6CE8;&#x518C; model</li>
<li>app.router(({ history, app }) =&gt; RouterConfig) &#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;</li>
<li>app.start(selector?) &#x542F;&#x52A8;&#x5E94;&#x7528;&#x3002;selector &#x53EF;&#x9009;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> dva, { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;dva&apos;</span>;

<span class="hljs-comment">// 1. Initialize</span>
<span class="hljs-keyword">const</span> app = dva();

<span class="hljs-comment">// 2. Model</span>
app.model({
  <span class="hljs-attr">namespace</span>: <span class="hljs-string">&apos;count&apos;</span>,
  <span class="hljs-attr">state</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">reducers</span>: {
    add  (count) { <span class="hljs-keyword">return</span> count + <span class="hljs-number">1</span> },
    minus(count) { <span class="hljs-keyword">return</span> count - <span class="hljs-number">1</span> },
  },
});

<span class="hljs-comment">// 3. View</span>
<span class="hljs-keyword">const</span> App = connect(<span class="hljs-function">(<span class="hljs-params">{ count }</span>) =&gt;</span> ({
  count
}))(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>{ props.count }<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> { props.dispatch({type: &apos;count/add&apos;})}}&gt;+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;minus&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> { props.dispatch({type: &apos;count/minus&apos;})}}&gt;-<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
});

<span class="hljs-comment">// 4. Router</span>
app.router(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> &lt;App /&gt;);

<span class="hljs-comment">// 5. Start</span>
app.start(<span class="hljs-string">&apos;#root&apos;</span>);
</code></pre>
<h2 id="t136. Model">6. Model <a href="#t136. Model"> # </a></h2>
<p>model &#x662F; dva &#x4E2D;&#x6700;&#x91CD;&#x8981;&#x7684;&#x6982;&#x5FF5;</p>
<pre><code class="lang-js">import React,{Fragment} from &apos;react&apos;;
import dva,{connect} from &apos;dva&apos;;
import {Route,Router} from &apos;dva/router&apos;;
const app=dva();
app.model({
    namespace: &apos;todos&apos;,
    state: {
        list: [],
        filter:&apos;all&apos;
    },
    reducers: {
        loaded(state,{payload:list}) {
            return {
                ...state,list:list
            }
        },
        added(state,{payload:todo}) {
            return {
                ...state,list: [...state.list,{...todo,id:Date.now(),completed:false}]
            }
        },
        toggled(state,{payload:id}) {
            return {
                ...state,
                list: state.list.map(item =&gt; {
                    if (item.id===id) {
                        item.completed=!item.completed;
                    }
                    return item;
                })
            }
        }
    },
    effects: {
        *load({},{put,call}) {
            const list=[{id:1,text:&apos;1&apos;,completed:false},{id:2,text:&apos;2&apos;,completed:false}];
            yield put({type:&apos;loaded&apos;,payload:list});
        },
        *add({payload},{put,call}) {
            yield put({type:&apos;added&apos;,payload});
        },
        *toggle({payload},{put,call}) {
            yield put({type:&apos;toggled&apos;,payload});
        }
    },
    subscriptions: {
        setup({history,dispatch}) {
            history.listen(({pathname}) =&gt; {
                if (pathname==&apos;/todos&apos;) {
                    dispatch({type:&apos;load&apos;});
                }
            });
        }
    }
});
const Home=() =&gt; &lt;div&gt;Home&lt;/div&gt;
let Todos=({list,filter,toggle,add}) =&gt; (
        &lt;div&gt;
        &lt;input ref={input =&gt; this.text=input} /&gt;
        &lt;button onClick={() =&gt; {add(this.text.value);this.text.value = &apos;&apos;}}&gt;+&lt;/button&gt;
            &lt;ul&gt;
                {list.map(item =&gt; (
                &lt;li&gt;
                    &lt;input type=&quot;checkbox&quot; checked={item.completed} onChange={toggle.bind(null,item.id)}/&gt;
                    {item.text}
                    &lt;/li&gt;
                ))}
            &lt;/ul&gt;
        &lt;/div&gt;
)
let actions={
    add(text) {
        return {type:&apos;todos/add&apos;,payload:{text}}
    },
    toggle(id) {
        return {type:&apos;todos/toggle&apos;,payload:id}
    }
}
Todos=connect(
    state =&gt; ({
        list: state.todos.list.filter(todo =&gt; {
            switch (state.filter) {
                case &apos;completed&apos;:
                    return todo.completed;
                case &apos;uncompleted&apos;:
                    return !todo.completed
                default:
                    return true;
            }
        }),
        filter:state.filter
    }),
    actions
)(Todos);
app.router(({history,app}) =&gt; (
    &lt;Router history={history}&gt;
        &lt;Fragment&gt;
            &lt;Route exact path=&quot;/&quot; component={Home} /&gt;
            &lt;Route path=&quot;/todos&quot; component={Todos} /&gt;
        &lt;/Fragment&gt;
    &lt;/Router&gt;
));
app.start(&apos;#root&apos;);
</code></pre>
<ul>
<li>namespace model &#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x662F;&#x4ED6;&#x5728;&#x5168;&#x5C40; state &#x4E0A;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x53EA;&#x80FD;&#x7528;&#x5B57;&#x7B26;&#x4E32;</li>
<li>state &#x521D;&#x59CB;&#x503C;</li>
<li>reducers &#x4EE5; key/value &#x683C;&#x5F0F;&#x5B9A;&#x4E49; reducer&#x3002;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x552F;&#x4E00;&#x53EF;&#x4EE5;&#x4FEE;&#x6539; state &#x7684;&#x5730;&#x65B9;&#x3002;&#x7531; action &#x89E6;&#x53D1;&#x3002;</li>
<li>effects &#x4EE5; key/value &#x683C;&#x5F0F;&#x5B9A;&#x4E49; effect&#x3002;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#xFF0C;&#x4E0D;&#x76F4;&#x63A5;&#x4FEE;&#x6539; state&#x3002;&#x7531; action &#x89E6;&#x53D1;&#xFF0C;&#x53EF;&#x4EE5;&#x89E6;&#x53D1; action&#xFF0C;&#x53EF;&#x4EE5;&#x548C;&#x670D;&#x52A1;&#x5668;&#x4EA4;&#x4E92;&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5168;&#x5C40; state &#x7684;&#x6570;&#x636E;&#x7B49;&#x7B49;&#x3002;</li>
<li>subscriptions &#x4EE5; key/value &#x683C;&#x5F0F;&#x5B9A;&#x4E49; subscription&#x3002;subscription &#x662F;&#x8BA2;&#x9605;&#xFF0C;&#x7528;&#x4E8E;&#x8BA2;&#x9605;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x6E90;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x9700;&#x8981; dispatch &#x76F8;&#x5E94;&#x7684; action&#x3002;&#x5728; app.start() &#x65F6;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x6570;&#x636E;&#x6E90;&#x53EF;&#x4EE5;&#x662F;&#x5F53;&#x524D;&#x7684;&#x65F6;&#x95F4;&#x3001;&#x670D;&#x52A1;&#x5668;&#x7684; websocket &#x8FDE;&#x63A5;&#x3001;keyboard &#x8F93;&#x5165;&#x3001;geolocation &#x53D8;&#x5316;&#x3001;history &#x8DEF;&#x7531;&#x53D8;&#x5316;&#x7B49;&#x7B49;&#x3002;</li>
</ul>
<h2 id="t147. &#x6D4B;&#x8BD5;&#x9F20;&#x6807;&#x70B9;&#x51FB;&#x901F;&#x5EA6;&#x7684;&#x7684;&#x5E94;&#x7528;">7. &#x6D4B;&#x8BD5;&#x9F20;&#x6807;&#x70B9;&#x51FB;&#x901F;&#x5EA6;&#x7684;&#x7684;&#x5E94;&#x7528; <a href="#t147. &#x6D4B;&#x8BD5;&#x9F20;&#x6807;&#x70B9;&#x51FB;&#x901F;&#x5EA6;&#x7684;&#x7684;&#x5E94;&#x7528;"> # </a></h2>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x9F20;&#x6807;&#x70B9;&#x51FB;&#x901F;&#x5EA6;&#x7684; App&#xFF0C;&#x8BB0;&#x5F55; 1 &#x79D2;&#x5185;&#x7528;&#x6237;&#x80FD;&#x6700;&#x591A;&#x70B9;&#x51E0;&#x6B21;&#x3002;&#x9876;&#x90E8;&#x7684; Highest Record &#x7EAA;&#x5F55;&#x6700;&#x9AD8;&#x901F;&#x5EA6;&#xFF1B;&#x4E2D;&#x95F4;&#x7684;&#x662F;&#x5F53;&#x524D;&#x901F;&#x5EA6;&#xFF0C;&#x7ED9;&#x4E88;&#x5373;&#x65F6;&#x53CD;&#x9988;&#xFF0C;&#x8BA9;&#x7528;&#x6237;&#x66F4;&#x6709;&#x53C2;&#x4E0E;&#x611F;&#xFF1B;&#x4E0B;&#x65B9;&#x662F;&#x4F9B;&#x70B9;&#x51FB;&#x7684;&#x6309;&#x94AE;&#x3002;</p>
<h3 id="t157.1 &#x5B89;&#x88C5;dva-cli">7.1 &#x5B89;&#x88C5;dva-cli <a href="#t157.1 &#x5B89;&#x88C5;dva-cli"> # </a></h3>
<pre><code class="lang-js">npm install -g dva-cli
dva -v
dva-h
</code></pre>
<h3 id="t167.2 &#x521B;&#x5EFA;&#x65B0;&#x5E94;&#x7528;">7.2 &#x521B;&#x5EFA;&#x65B0;&#x5E94;&#x7528; <a href="#t167.2 &#x521B;&#x5EFA;&#x65B0;&#x5E94;&#x7528;"> # </a></h3>
<pre><code class="lang-js">dva <span class="hljs-keyword">new</span> counterApp --demo
cd counterApp
npm start
</code></pre>
<h3 id="t177.3  &#x5B9A;&#x4E49;&#x6A21;&#x578B;">7.3  &#x5B9A;&#x4E49;&#x6A21;&#x578B; <a href="#t177.3  &#x5B9A;&#x4E49;&#x6A21;&#x578B;"> # </a></h3>
<pre><code class="lang-js">app.model({
  <span class="hljs-attr">namespace</span>: <span class="hljs-string">&apos;count&apos;</span>,
  <span class="hljs-attr">state</span>: {
    <span class="hljs-attr">record</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">current</span>: <span class="hljs-number">0</span>
  }
});
</code></pre>
<ul>
<li>namespace &#x662F; model state &#x5728;&#x5168;&#x5C40; state &#x6240;&#x7528;&#x7684; key</li>
<li>state &#x662F;&#x9ED8;&#x8BA4;&#x72B6;&#x6001;</li>
<li>state &#x91CC;&#x7684; record &#x8868;&#x793A; highest record</li>
<li>current &#x8868;&#x793A;&#x5F53;&#x524D;&#x901F;&#x5EA6;</li>
</ul>
<h3 id="t187.4 &#x5B8C;&#x6210;Component">7.4 &#x5B8C;&#x6210;Component <a href="#t187.4 &#x5B8C;&#x6210;Component"> # </a></h3>
<p>CountApp\index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./index.less&apos;</span>;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;dva&apos;</span>;
<span class="hljs-keyword">const</span> CountApp = <span class="hljs-function">(<span class="hljs-params">{ record, current, dispatch }</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.normal}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.record}</span>&gt;</span>&#x6700;&#x9AD8;&#x5206;:{record}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.current}</span>&gt;</span>&#x5F53;&#x524D;&#x5206;:{current}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{styles.button}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> dispatch({ type: &apos;count/add&apos; })}&gt;+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    )
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(<span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state.count)(CountApp);
</code></pre>
<p>CountApp\index.less</p>
<pre><code class="lang-css">.normal{
    width:250px;
    border:1px solid #CCC;
    box-shadow: 1px 1px 1px 2px #CCC,-1px -1px 1px 2px #CCC;
    margin:50px auto;
    padding:50px;
    .record{
        font-size:18px;
        color:#CCC;
        text-align: left;
        border-bottom: 1px solid #CCC;
    }
    .current{
        height:150px;
        line-height: 150px;
        text-align: center;
    }
    .button{
        text-align: center;
        button{
            width:100px;
            height:50px;
            background-color: #CCC;
            color:#FFF;
        }
    }
}
</code></pre>
<h3 id="t197.5 &#x66F4;&#x65B0;state">7.5 &#x66F4;&#x65B0;state <a href="#t197.5 &#x66F4;&#x65B0;state"> # </a></h3>
<ul>
<li>&#x66F4;&#x65B0; state &#x662F;&#x901A;&#x8FC7; reducers &#x5904;&#x7406;&#x7684;</li>
<li>reducer &#x662F;&#x552F;&#x4E00;&#x53EF;&#x4EE5;&#x66F4;&#x65B0; state &#x7684;&#x5730;&#x65B9;&#xFF0C;&#x8FD9;&#x4E2A;&#x552F;&#x4E00;&#x6027;&#x8BA9;&#x6211;&#x4EEC;&#x7684; App &#x66F4;&#x5177;&#x53EF;&#x9884;&#x6D4B;&#x6027;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#x4FEE;&#x6539;&#x90FD;&#x6709;&#x636E;&#x53EF;&#x67E5;&#x3002;reducer &#x662F; pure function&#xFF0C;&#x4ED6;&#x63A5;&#x6536;&#x53C2;&#x6570; state &#x548C; action&#xFF0C;&#x8FD4;&#x56DE;&#x65B0;&#x7684; state&#xFF0C;&#x901A;&#x8FC7;&#x8BED;&#x53E5;&#x8868;&#x8FBE;&#x5373; (state, action) =&gt; newState&#x3002;<blockquote>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x91CC;&#x7684; add &#x548C; minus &#x4E24;&#x4E2A;action&#xFF0C;&#x5728; count model &#x7684;&#x5B9A;&#x4E49;&#x4E2D;&#x662F;&#x4E0D;&#x9700;&#x8981;&#x52A0; namespace &#x524D;&#x7F00;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x81EA;&#x8EAB;&#x6A21;&#x578B;&#x4EE5;&#x5916;&#x662F;&#x9700;&#x8981;&#x52A0; model &#x7684; namespace</p>
</blockquote>
</li>
</ul>
<pre><code class="lang-js">app.model({
  <span class="hljs-attr">namespace</span>: <span class="hljs-string">&apos;count&apos;</span>,
  <span class="hljs-attr">state</span>: {
    <span class="hljs-attr">record</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">current</span>: <span class="hljs-number">0</span>
  },
  <span class="hljs-attr">reducers</span>: {
    add(state) {
      <span class="hljs-keyword">const</span> newCurrent = state.current + <span class="hljs-number">1</span>;
      <span class="hljs-keyword">return</span> {
        ...state,
        <span class="hljs-attr">record</span>: newCurrent &gt; state.record ? newCurrent : state.record,
        <span class="hljs-attr">current</span>: newCurrent
      }
    },
    minus(state) {
      <span class="hljs-keyword">return</span> { ...state, <span class="hljs-attr">current</span>: state.current - <span class="hljs-number">1</span> };
    }
  }
});
</code></pre>
<h3 id="t207.6 &#x5F02;&#x6B65;&#x5904;&#x7406;">7.6 &#x5F02;&#x6B65;&#x5904;&#x7406; <a href="#t207.6 &#x5F02;&#x6B65;&#x5904;&#x7406;"> # </a></h3>
<ul>
<li>dva &#x901A;&#x8FC7;&#x5BF9; model &#x589E;&#x52A0; effects &#x5C5E;&#x6027;&#x6765;&#x5904;&#x7406; side effect(&#x5F02;&#x6B65;&#x4EFB;&#x52A1;)</li>
<li>&#x8FD9;&#x662F;&#x57FA;&#x4E8E; redux-saga &#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x8BED;&#x6CD5;&#x4E3A; generator</li>
<li>&#x5F53;&#x7528;&#x6237;&#x70B9; + &#x6309;&#x94AE;&#xFF0C;&#x6570;&#x503C;&#x52A0; 1 &#x4E4B;&#x540E;&#xFF0C;&#x4F1A;&#x989D;&#x5916;&#x89E6;&#x53D1;&#x4E00;&#x4E2A; side effect&#xFF0C;&#x5373;&#x5EF6;&#x8FDF; 1 &#x79D2;&#x4E4B;&#x540E;&#x6570;&#x503C;&#x51CF;1 &#x3002;<pre><code class="lang-js">effects: {
  *add(action, { call, put }) {
    <span class="hljs-keyword">yield</span> call(delay, <span class="hljs-number">1000</span>);
    <span class="hljs-keyword">yield</span> put({ <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;minus&apos;</span> });
  }
},
</code></pre>
</li>
<li>call &#x548C; put &#x90FD;&#x662F; redux-saga &#x7684; effects&#xFF0C;call &#x8868;&#x793A;&#x8C03;&#x7528;&#x5F02;&#x6B65;&#x51FD;&#x6570;&#xFF0C;put &#x8868;&#x793A; dispatch action&#xFF0C;&#x5176;&#x4ED6;&#x7684;&#x8FD8;&#x6709; select, take, fork, cancel &#x7B49;&#xFF0C;&#x8BE6;&#x89C1; <a href="https://redux-saga-in-chinese.js.org/">redux-saga</a></li>
</ul>
<h3 id="t217.7 &#x8BA2;&#x9605;&#x952E;&#x76D8;&#x4E8B;&#x4EF6;">7.7 &#x8BA2;&#x9605;&#x952E;&#x76D8;&#x4E8B;&#x4EF6; <a href="#t217.7 &#x8BA2;&#x9605;&#x952E;&#x76D8;&#x4E8B;&#x4EF6;"> # </a></h3>
<p>ubscription &#x8BED;&#x4E49;&#x662F;&#x8BA2;&#x9605;&#xFF0C;&#x7528;&#x4E8E;&#x8BA2;&#x9605;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x6E90;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x6761;&#x4EF6; dispatch &#x9700;&#x8981;&#x7684; action&#x3002;&#x6570;&#x636E;&#x6E90;&#x53EF;&#x4EE5;&#x662F;&#x5F53;&#x524D;&#x7684;&#x65F6;&#x95F4;&#x3001;&#x670D;&#x52A1;&#x5668;&#x7684; websocket &#x8FDE;&#x63A5;&#x3001;keyboard &#x8F93;&#x5165;&#x3001;geolocation &#x53D8;&#x5316;&#x3001;history &#x8DEF;&#x7531;&#x53D8;&#x5316;&#x7B49;&#x7B49;&#x3002;</p>
<ul>
<li><a href="https://www.npmjs.com/package/keymaster">keymaster</a></li>
</ul>
<pre><code class="lang-js">subscriptions: {
    keyboard({ dispatch }) {
      key(<span class="hljs-string">&apos;space&apos;</span>, () =&gt; dispatch({ <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;add&apos;</span> }));
    }
  }
</code></pre>
<h3 id="t227.8 &#x6784;&#x5EFA;&#x5E94;&#x7528;">7.8 &#x6784;&#x5EFA;&#x5E94;&#x7528; <a href="#t227.8 &#x6784;&#x5EFA;&#x5E94;&#x7528;"> # </a></h3>
<pre><code class="lang-js">$ npm run build
</code></pre>
<h2 id="t238. &#x53C2;&#x8003;">8. &#x53C2;&#x8003; <a href="#t238. &#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://dvajs.com/">dvajs</a></li>
<li><a href="https://www.npmjs.com/package/dva">dva-npm</a></li>
<li><a href="https://github.com/dvajs/dva">dva-github</a></li>
<li><a href="https://github.com/dvajs/dva/blob/master/docs/Concepts_zh-CN.md">8&#x4E2A;&#x6982;&#x5FF5;</a></li>
<li><a href="http://cn.redux.js.org/index.html">redux</a></li>
<li><a href="https://redux-saga-in-chinese.js.org/">redux-saga</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2015/04/generator.html">generator</a></li>
<li></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
